#include    "spritesheetrender.h"
#include    "../common.h"

SpriteSheetRender::SpriteSheetRender():
m_sheet(0),
m_showIndex(0)
{

}

SpriteSheetRender::~SpriteSheetRender()
{

}

void SpriteSheetRender::update()
{
    if(m_sheet == 0)
    {
        PERSONAL_DEBUG_LOG("sprite sheet is null!");
        return;
    }

    m_showIndex++;

    unsigned int maxIndex = m_sheet->getNumberOfSheet();

    if(m_showIndex >= maxIndex)
    {
        m_showIndex = 0;
    }
}


void SpriteSheetRender::render(GLfloat x,GLfloat y)
{
    if(m_sheet == 0)
    {
        PERSONAL_DEBUG_LOG("SpriteSheetPicture not set!");
        return;
    }

    if(m_useTransparency)
    {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    }


    Point position{x,y};
    Size picSize = m_sheet->getSize();

    PersonalTexture::Texture2D::DrawTexture(m_sheet->getTexture(),&position,&picSize,m_sheet->getTextureArea(m_showIndex));


    if(m_useTransparency)
    {
        glDisable(GL_BLEND);
    }
}

void SpriteSheetRender::renderReverse(GLfloat x,GLfloat y)
{
    if(m_sheet == 0)
    {
        PERSONAL_DEBUG_LOG("SpriteSheetPicture not set!");
        return;
    }


    if(m_useTransparency)
    {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    }


    Point position{x,y};
    Size picSize = m_sheet->getSize();

    PersonalTexture::Texture2D::DrawReverseTexture(m_sheet->getTexture(),&position,&picSize,m_sheet->getTextureArea(m_showIndex));

    if(m_useTransparency)
    {
        glDisable(GL_BLEND);
    }
}
